const api = require('../../config/supplierStore.js')
const apiCommite = require('../../config/wechatcommite.js');
Page({
	/**
   * 页面的初始数据
   */
	data: {
		showProp: false,
		radioone: '1',
		radiotwo: '1',
		radiothree: '1',
		radiofour: '1',
		radiofive: '1',
		radiosix: '1',
		radioseven: '1',
		radioeight: '1',
		radionine: '1',
		radioten: '1',
		choose: '',
		cIndex: '',
		show: false,
		columns: [],
		brand: '',
		material: '',
		price: '',
		phoneTag: false,
		passData: '',
		showWeChat: true,
		list: [],
		barData: []
	},
	lookBigPic(e) {
		let srcs = []
		let src = e.currentTarget.dataset.src
		srcs.push(src)
		wx.previewImage({
			urls: srcs,
		})
	},
	onChange(e) {
		const { tag } = e.target.dataset
		this.setData({
			[tag]: e.detail
		})
	},
	onChangeChoose(e) {
	},
	showPopup(e) {
		//点击选择性别，打开弹出层（选择器）
		this.setData({
			show: true
		})
	},
	onClose() {
		//点击空白处开闭弹出层（选择器）及选择器左上角的取消
		this.setData({
			show: false
		})
	},
	onConfirm(e) {
		//选择器右上角的确定，点击确定获取值
		this.setData({
			choose: e.detail.value,
			show: false
		})
		this.data.cIndex.forEach((element) => {
			if (element.dictLabel === e.detail.value) {
				this.setData({
					passData: element
				})
			}
		})
	},
	onChangeBrand(e) {
		this.setData({
			brand: e.detail
		})
	},
	onChangeMaterial(e) {
		this.setData({
			material: e.detail
		})
	},
	onChangePrice(e) {
		this.setData({
			price: e.detail
		})
	},
	clickCnnect(e) {
		const { phonetag } = e.target.dataset
		if (phonetag === '0') {
			this.setData({
				phone: '',
				phoneTag: false
			})
		} else {
			this.setData({
				phone: '',
				phoneTag: true
			})
		}
	},
	onChangePhone(e) {
		this.setData({
			phone: e.detail
		})
	},
	submite() {
		let bool = this.valided()
		if (!bool) {
			return
		}
		const userid = wx.getStorageSync('wxUser') ? wx.getStorageSync('wxUser').id : ''
		if (userid != '') {
			wx.request({
				url: api.submit, //仅为示例，并非真实接口地址。
				data: {
					establish: Number(this.data.radioone),
					storageStandard: Number(this.data.radiotwo),
					evaluationSystem: Number(this.data.radiothree),
					routineMaintenance: Number(this.data.radiofour),
					blacklist: Number(this.data.radiofive),
					quality: Number(this.data.radiosix),
					speed: Number(this.data.radioseven),
					securitys: Number(this.data.radioeight),
					businessAffairs: Number(this.data.radionine),
					awardWinning: Number(this.data.radioten),
					classifyId: Number(this.data.passData.dictValue),
					brand: this.data.brand,
					model: this.data.material,
					content: this.data.price,
					customer: this.data.phoneTag ? 0 : 1,
					contact: this.data.phone,
					userId: userid,
					createTime: ''
				},
				method: 'POST',
				success: (res) => {
					if (res.data.code === 200) {
						wx.showToast({
							title: '提交成功！',
							icon: 'success',
							duration: 2000
						})
						this.clearnData()
					}
				}
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '请先登录',
				success: function (res) {
					if (res.confirm) {
						wx.switchTab({
							url: '/pages/my/index'
						})
					} else if (res.cancel) {
					}
				}
			})
		}
	},

	clearnData() {
		this.setData({
			radioone: '1',
			radiotwo: '1',
			radiothree: '1',
			radiofour: '1',
			radiofive: '1',
			radiosix: '1',
			radioseven: '1',
			radioeight: '1',
			radionine: '1',
			radioten: '1',
			choose: '',
			brand: '',
			material: '',
			price: '',
			passData: '',
			phone: '',
		})
	},
	getCheckList() {
		wx.request({
			url: api.checkList, //仅为示例，并非真实接口地址。
			data: {
				type: 'address_supplier_category'
			},
			method: 'GET',
			success: (res) => {
				if (res.data.code === 200) {
					let obj = res.data.data
					let imgs = obj.map((item) => {
						return item.dictLabel
					})
					this.setData({
						columns: imgs
					})
					this.setData({
						choose: imgs[0]
					})
					this.setData({
						cIndex: obj
					})
					this.setData({
						passData: obj[0]
					})
				}
			}
		})
	},
	getList() {
		wx.request({
			url: apiCommite.list, //仅为示例，并非真实接口地址。
			data: {
				value: 0
			},
			method: 'GET',
			success: (res) => {
				console.log(res, res.data);

				if (res.data.code === 100) {
					let obj = res.data.object
					this.setData({
						list: obj
					})
				} else if (res.data.code === 200) {
					wx.showToast({
						title: res.data.msg
					})
				}

			}
		});
	},
	onChangeTab(e) {
		console.log(e);
		let {
			index
		} = e.detail
		if (index === 0) {
			this.setData({
				showWeChat: true
			})
		} else {
			this.setData({
				showWeChat: false
			})
		}

	},
	getName() {
		this.setData({
			barData: [{
				id: 1,
				dictLabel: '微信交流群',
				dictValue: '0',
			},
			{
				id: 2,
				dictLabel: '询价服务',
				dictValue: '1',
			}
			]
		})
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		this.getName()
		this.getList()
		this.getCheckList()
	},

	/**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () { },
	valided() {
    this.setData({
      showProp: false
    })
    let hasUser = getApp().hasUser()
    console.log("hasUser status", hasUser)
    if (!hasUser) {
      this.setData({
        showProp: true,
        onConfirmId: 0
      })
      console.log("hasUser", this.data.onConfirmId)
      return false
    } else {
      let hasPhone = getApp().hasPhone()
      console.log("hasPhone status", hasPhone)
      if (!hasPhone) {
        let user = wx.getStorageSync('wxUser')
        let wxName = user.wxName
        let wxUrl = user.wxUrl
        console.log("hasPhone user ", user)
        this.setData({
          showProp: true,
          onConfirmId: 1,
          userInfo: {
            avatarUrl: wxUrl,
            nickName: wxName
          }
        })
        return false
      }
      console.log("hasPhone", this.data.onConfirmId)
    }
    return true
  },
	// valided() {
	// 	this.setData({
	// 		showProp: false
	// 	})
	// 	let hasUser = getApp().hasUser()
	// 	if (!hasUser) {
	// 		this.setData({
	// 			showProp: true
	// 		})
	// 		return false
	// 	} else {
	// 		this.setData({
	// 			showProp: false
	// 		})
	// 		return true
	// 	}
	// },
	/**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		this.valided()
	},

	/**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () { 
		this.clearnData()
	},

	/**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () { },

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () { },

	/**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () { },

	/**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () { }
})
